/* //device/system/broncho-ril/misc.h
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

/** returns 1 if line starts with prefix, 0 if it does not */

#ifndef BRONCHO_RIL_H
#define BRONCHO_RIL_H

#include "atchannel.h"
#include <telephony/broncho-ril-socket.h>

#define BASEBAND_PREFIX_1_0_3 "M1190_V1.0.3"
#define BASEBAND_PREFIX_1_0_8 "M0282BA_DZSD_V1.0.0"
//#define BASEBAND_PREFIX_1_0_8 "M1190_V1.0.8"

struct _RIL;
typedef struct _RIL RIL;

typedef enum _RilSimState
{
	RIL_SIM_ABSENT,
	RIL_SIM_NOT_READY,
	RIL_SIM_READY,
	RIL_SIM_PIN,
	RIL_SIM_PUK,
	RIL_SIM_NETWORK_PERSONALIZATION
}RilSimState;

void RIL_pollSIMState (RIL* thiz);
int RIL_GetState (RIL* thiz);
void RIL_SetRadioState(RIL* thiz, RIL_RadioState newState);
void RIL_requestTimedCallback (RIL* thiz, RIL_TimedCallback callback, void *param,
                                const struct timeval *relativeTime);
void RIL_onUnsolicitedResponse(RIL* thiz, int unsolResponse, const void *data, 
                                size_t datalen);
void RIL_onRequestComplete(RIL* thiz, RIL_Token t, RIL_Errno e, void *response, size_t responselen);

ATChannel* RIL_GetDataChannel(RIL* thiz);
ATChannel* RIL_GetAtChannel(RIL* thiz);
void RIL_OpenDataChannel(RIL* thiz);
void RIL_CloseDataChannel(RIL* thiz);
int RIL_PowerOff(RIL* thiz);
int getSimId();
int RIL_GetDataChannelDeviceName(RIL* thiz, char* name);

void RIL_SetPrivInfo(RIL* thiz, void* info, const char* id);
void* RIL_GetPrivInfo(RIL* thiz, const char* id);

int decline_rild_restart_remained_time();
void resume_rild_restart_remained_time();

#define SIM_ID_DUAL 1

#define CHANNEL_AT_DSDS(thiz) (((thiz)->sim_id == SIM_ID_DUAL) ? CHANNEL_AT_SIM2 : CHANNEL_AT)

#define CHANNEL_DATA_DSDS(thiz) (((thiz)->sim_id == SIM_ID_DUAL) ? CHANNEL_DATA_SIM2 : CHANNEL_DATA)


#define ril_send_command_singleline(thiz, command, prefix, pp_resp)	\
			at_send_command_singleline(RIL_GetAtChannel(thiz), (command), DEFAULT_AT_TIMEOUT_MSEC, (prefix), (pp_resp))

#define	ril_send_command_numeric(thiz, command, pp_resp)	\
			at_send_command_numeric(RIL_GetAtChannel(thiz), (command), DEFAULT_AT_TIMEOUT_MSEC, (pp_resp))

#define ril_send_command_multiline(thiz, command, prefix, pp_resp)	\
			at_send_command_multiline(RIL_GetAtChannel(thiz), (command), DEFAULT_AT_TIMEOUT_MSEC, (prefix), (pp_resp))

#define ril_send_command(thiz, command, pp_resp)	\
			at_send_command(RIL_GetAtChannel(thiz), (command), DEFAULT_AT_TIMEOUT_MSEC, (pp_resp))

#define ril_send_command_sms(thiz, command, pdu, prefix, pp_resp)	\
			at_send_command_sms(RIL_GetAtChannel(thiz), (command), (pdu), (prefix), (pp_resp))

#define ril_command_exec_for_result(thiz, cmd)	\
			at_command_exec_for_result(RIL_GetAtChannel(thiz), (cmd), DEFAULT_AT_TIMEOUT_MSEC)

#define ril_send_command_singleline_full(thiz, command, t, prefix, pp_resp)	\
			at_send_command_singleline(RIL_GetAtChannel(thiz), (command), (t), (prefix), (pp_resp))

#define	ril_send_command_numeric_full(thiz, command, t, pp_resp)	\
			at_send_command_numeric(RIL_GetAtChannel(thiz), (command), (t), (pp_resp))

#define ril_send_command_multiline_full(thiz, command, t, prefix, pp_resp)	\
			at_send_command_multiline(RIL_GetAtChannel(thiz), (command), (t), (prefix), (pp_resp))

#define ril_send_command_full(thiz, command, t, pp_resp)	\
			at_send_command(RIL_GetAtChannel(thiz), (command), (t), (pp_resp))

#define ril_send_command_sms(thiz, command, pdu, prefix, pp_resp)	\
			at_send_command_sms(RIL_GetAtChannel(thiz), (command), (pdu), (prefix), (pp_resp))

#define ril_command_exec_full_for_result(thiz, cmd, t)	\
			at_command_exec_for_result(RIL_GetAtChannel(thiz), (cmd), (t))

#define ril_command_exec_singleline_for_result(thiz, cmd, prefix, pp_resp)	\
			at_command_exec_singleline_for_result(RIL_GetAtChannel(thiz), (cmd), DEFAULT_AT_TIMEOUT_MSEC, (prefix), (pp_resp))

#define ril_command_exec_singleline_full_for_result(thiz, cmd, t, prefix, pp_resp)	\
			at_command_exec_singleline_for_result(RIL_GetAtChannel(thiz), (cmd), (t), (prefix), (pp_resp))


/*broncho yinlijun add for A13 ril interface 20121029 */
#define RIL_CLIENT_LOCK(client) pthread_mutex_lock(&(client->mutex));
#define RIL_CLIENT_UNLOCK(client) pthread_mutex_unlock(&(client->mutex));

struct ril_client;
extern struct ril_client *srs_client;

/**
 * RIL client
 */

typedef int (*ril_client_func)(struct ril_client *client);

typedef enum {
	RIL_CLIENT_NULL		= 0,
	RIL_CLIENT_CREATED	= 1,
	RIL_CLIENT_READY	= 2,
	RIL_CLIENT_DESTROYED	= 3,
	RIL_CLIENT_ERROR	= 4,

} ril_client_state;

struct ril_client {
	ril_client_func create;
	ril_client_func destroy;
	ril_client_func read_loop;

	void *object;

	pthread_t thread;
	pthread_mutex_t mutex;

	ril_client_state state;
};

struct ril_client_funcs {
	ril_client_func create;
	ril_client_func destroy;
	ril_client_func read_loop;
};

struct ril_client *ril_client_new(struct ril_client_funcs *client_funcs);
int ril_client_free(struct ril_client *client);
int ril_client_create(struct ril_client *client);
int ril_client_destroy(struct ril_client *client);
int ril_client_thread_start(struct ril_client *client);
void srs_dispatch(int fd, struct srs_message *message);

/*broncho yinlijun add end */
#endif

